package se19team1e.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.JobResponseDAO;
import se19team1e.dao.factory.DAOFactory;
import se19team1e.dao.factory.DAOFactorySelector;
import se19team1e.entity.JobResponseDTO;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.BaseScreen;
import se19team1e.gui.JobAdvertResponseResultScreen;
import se19team1e.gui.JobAdvertSearchForm;

/**
 * This controller will allow users to view the job advert response results. It
 * extends from the base controller
 * 
 * @author Tay Toh Tong
 * 
 */
public class ViewJobResponseResultController extends BaseController {
	private Application application;

	/**
	 * Calls the super constructor for the base controller
	 * 
	 * @param application
	 */
	public ViewJobResponseResultController(Application application) {
		super(application);
		this.application = application;
	}

	/**
	 * Creates the job advert response result screen with with an existing job
	 * advert ID
	 * 
	 * @param jobAdvertID
	 *            Response for specific job advert
	 */
	public void initialize(String jobAdvertID) {

		// get screen
		JobAdvertResponseResultScreen screen = this.getApplication().getScreen(
				JobAdvertResponseResultScreen.class);

		// Retrieve RelaventJobResponse
		List<JobResponseDTO> jobResponses = null;
		try {
			jobResponses = this.retriveJobResponseByJobAdvertID(jobAdvertID);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}

		// initialize screen
		screen.initialize(this, jobResponses);

		// set as active screen
		this.getApplication().setCurrentScreen(screen);

	}

	/**
	 * Retrieves all Job response Related to the JobAdvert.
	 * 
	 * @param jobID
	 *            The unique job id
	 * @return List of Job Response DTO or null when relevant Job Responses is
	 *         zero
	 * @throws DataAccessException
	 */
	public List<JobResponseDTO> retriveJobResponseByJobAdvertID(String jobID)
			throws DataAccessException {
		List<JobResponseDTO> relaventJobResponses = new ArrayList<JobResponseDTO>();

		// Retrieve JobResponseDAO
		DAOFactory daoFactory = DAOFactorySelector.getDAOFactory();
		JobResponseDAO jobResponseDAO = daoFactory.getJobResponseDAO();

		List<JobResponseDTO> allJobResponse = jobResponseDAO.retrieveAll();

		Iterator<JobResponseDTO> i = allJobResponse.iterator();
		// go through all of them and select the relevant ones
		while (i.hasNext()) {
			JobResponseDTO jobResponse = i.next();
			if (jobResponse.getJobAdvertID().equalsIgnoreCase(jobID)) {
				relaventJobResponses.add(jobResponse);
			}
		}

		if (relaventJobResponses.size() > 0) {
			return relaventJobResponses;
		}
		return null;

	}

	/**
	 * Retrieve the Job Response DTO by its unique ID
	 * 
	 * @param id
	 *            The criteria of search is Job Response ID
	 * @return Job Response object
	 * @exception DataAccessException
	 *                If no id is found
	 */
	public JobResponseDTO retriveJobResponseByID(String id) {
		// Retrieve JobResponseDAO
		DAOFactory daoFactory = DAOFactorySelector.getDAOFactory();
		JobResponseDAO jobResponseDAO = daoFactory.getJobResponseDAO();

		JobResponseDTO jobResponse = null;
		try {
			jobResponse = jobResponseDAO.retrieveById(id);
			System.out.println(jobResponse);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jobResponse;
	}

	/**
	 * To return control of GUI to the previous screen
	 */
	public void backToPreviousScreen() {
		BaseScreen previousScreen = application
				.getScreen(JobAdvertSearchForm.class);
		application.setCurrentScreen(previousScreen);
	}
}
